package org.skydiveapp2;

import org.skydiveapp2.ui.InterfaceManager;
import org.skydiveapplibrary.repository.RigRepository;
import org.skydiveapplibrary.ui.GearListAdapter;

import android.content.Intent;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;

public class GearListFragment extends BaseExpandableListFragment
{  
    @Override
    public void onResume()
    {
    	super.onResume();
    	
    	getExpandableListView().setChoiceMode(ListView.CHOICE_MODE_NONE);
    	
    	InterfaceManager.getInstance().setSubtitleText(getActivity(), R.string.gear_list_label);

    	setHasOptionsMenu(true);
    	
    	// always re-load data
    	loadData();
    }
    
    @Override
    public boolean onRigClick(android.widget.ExpandableListView parent, View v, int groupPosition, int childPosition, long id)
    {
        // start edit
        Intent intent = new Intent(getActivity(), RigEditActivity.class);
        intent.putExtra(Constants.EXTRA_RIG_ID, (int)id);
        intent.putExtra(Constants.EXTRA_SEMI_TRANSPARENT_THEME,true);
        startActivity(intent);
        return true;
    }
    
    public void loadData()
    {
    	// get repos
    	RigRepository rigRepository = new RigRepository(getActivity());
    	GearListAdapter adapter = new GearListAdapter(
    			getActivity(),
    			rigRepository.getRigs(),
    			rigRepository.getArchivedRigs());
    	
    	// set adapter
		setListAdapter(adapter);
		
		// expand groups
		for (int group = 0; group < adapter.getGroupCount(); group++)
		{
			getExpandableListView().expandGroup(group);
		}
    }
    
    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        inflater.inflate(R.menu.menu_add, menu);
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle item selection
        switch (item.getItemId())
        {
        case R.id.menu_add:        	
        	Intent intent = new Intent(getActivity(), RigEditActivity.class);
        	intent.putExtra(Constants.EXTRA_SEMI_TRANSPARENT_THEME, true);
            startActivity(intent);
            return true;
        
        default:
            return super.onOptionsItemSelected(item);
        }
    }
} 